import { Company, CompanyProp } from '@/types/company';
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { CompanyHandler } from '@/types/tracker';

export function useCompanyList(handlers: CompanyHandler) {
    const current = ref({} as Company);
    const show = ref(false);
    const isAdd = ref(false);

    const modifyCompany = (company: Company) => {
        isAdd.value = false;
        current.value = new Company(company);
        show.value = true;
    };
    const deleteCompany = (company: Company) => {
        ElMessageBox.confirm(`从追踪池中删除"${company.name}"，是否继续?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        })
            .then(() => {
                handlers.removeCompany(company);
                ElMessage.success({
                    message: '删除成功!',
                });
            })
            .catch(() => {
                console.log('cancel delete!');
            });
    };
    const addCompany = () => {
        isAdd.value = true;
        current.value = new Company({ code: '', name: '' });
        show.value = true;
    };

    const updateCompany = (company: Company) => {
        handlers.updateCompany(company);
    };

    return {
        show,
        isAdd,
        current,
        addCompany,
        modifyCompany,
        updateCompany,
        deleteCompany,
    };
}
